package com.wangyadong.hobby.schedule.jobtask.dataa;



/**
 * @ClassName ScoreboardSelf
 * @Description TODO
 * @Authpr Antonio
 * @Data 2018-06-14 10:34
 */
public class ScoreboardSelf {

    private GameEntry[] array = null;
    private int index = 0;


    public ScoreboardSelf(int ca) {
        array = new GameEntry[ca];
    }

    public void add(GameEntry e) {
        if (array.length > (index - 1) || e.getScore() > array[index - 1].getScore()) {
            if (index < array.length)
                index++;
        }
        int j = index - 1;
        while (j > 0 && e.getScore() > array[j - 1].getScore()) {
            array[j] = array[j - 1];
            j--;
        }
        array[j] = e;
    }

    public GameEntry remove(int index) {
        if (index < 0 || index > array.length) {
            throw new IndexOutOfBoundsException("out of the index");
        }
        GameEntry gameEntry = array[index];
        array[index] = null;
        for (int i = index; i < array.length - 1; i++) {
            array[i] = array[i + 1];
        }
        array[index - 1] = null;
        index--;
        return gameEntry;
    }

    public static void main(String[] args) {
        ScoreboardSelf scoreboard = new ScoreboardSelf(3);
        scoreboard.add(new GameEntry("wan-0", 33));
        scoreboard.add(new GameEntry("wan-1", 35));
        scoreboard.add(new GameEntry("wan-2", 40));
        scoreboard.add(new GameEntry("wan-3", 41));
        System.out.println(scoreboard.index);
        scoreboard.remove(1);


    }

    public static void insertionSort(char[] data) {
        int n = data.length;
        for (int k = 1; k < n; k++) { // begin with second character
            char cur = data[k]; // time to insert cur=data[k]
            int j = k; // find correct index j for cur
            while (j > 0 && data[j-1] >cur){ // thus, data[j-1] must go after cur
                data[j] = data[j-1]; // slide data[j-1] rightward
                j--; // and consider previous j for cur
            }
            data[j] = cur; // this is the proper place for cur
        }
    }
}
